﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GodHelper.Models;
using GodHelper.Util;
using BLL;

namespace GodHelper.Controllers
{
    public class Mange_MainController : Controller
    {
        //
        // GET: /Mange_Main/

        //public ActionResult Index()
        //{
        //    return View();
        //}


        /// <summary>
        /// 添加任务
        /// </summary>
        /// <returns></returns>
        //[HttpPost]
        public ActionResult AddTask(String str_task_decript, String str_task_time, String str_task_type, String str_task_state, String str_task_contact, string str_task_email)
        {
            View_MainController vm = new View_MainController();
            str_task_state = BLL.DefaultDB.DF_TASKSTATE_ING;
            string taskid = Business.MainMange.add_task(str_task_decript, str_task_time, str_task_type, str_task_state, str_task_contact, str_task_email);
            if (taskid != null)
            {
                return Json(new { Result = true, Message = taskid });
            }

            return Json(new { Result = false });
        }

        /// <summary>
        /// 完成任务
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult FinishTask(String taskid)
        {
            if (Business.MainMange.finish_task(taskid))
            {
                return Json(new { Result = true });
            }
            return Json(new { Result = false });
        }

        /// <summary>
        /// 删除任务
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DeleteTask(String taskid)
        {
            if (Business.MainMange.delete_task(taskid))
            {
                return Json(new { Result = true });
            }
            return Json(new { Result = false });
        }

        /// <summary>
        /// 删除任务
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult ChangeTask(String taskid, String str_task_decript, String str_task_time, String str_task_type, String str_task_contact)
        {
            if (Business.MainMange.change_task(taskid,str_task_decript,str_task_time, str_task_type, str_task_contact))
            {
                return Json(new { Result = true });
            }
            return Json(new { Result = false });
        }


        /// <summary>
        /// 获取所有联系人
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetContact(string email)
        {
            List<contact> ct = UserModelManager.instance.get(email).UserModel_contact;

            int count = ct.Count;

            if ((ct == null) || (count < 1))
            {
                return Json(new { Result = false });
            }

            List<string> contact_id = new List<string>();
            List<string> contact_name = new List<string>();
            List<string> contact_phone = new List<string>();
            List<string> contact_image = new List<string>();
            List<string> contact_time = new List<string>();
            List<string> contact_refrences_user_email = new List<string>();
            for (int i = 0; i < ct.Count; i++)
            {
                string str = "" + ct[i].contact_id;
                contact_id.Add(str);
                string str1 = "" + ct[i].contact_name;
                contact_name.Add(str1);
                string str2 = "" + ct[i].contact_phone;
                contact_phone.Add(str2);
                string str3 = "" + ct[i].contact_image;
                contact_image.Add(str3);
                string str4 = "" + ct[i].contact_time;
                contact_time.Add(str4);
                string str5 = "" + ct[i].contact_refrences_user_email;
                contact_refrences_user_email.Add(str5);
            }
           
                //if (Business.MainMange.change_task(taskid, str_task_decript, str_task_time, str_task_type, str_task_contact))
                //{
                //    return Json(new { Result = true });
                //}
            return Json(new { Result = true, count, contact_id, contact_name, contact_phone, contact_image, contact_time,contact_refrences_user_email });
        }

        /// <summary>
        /// 修改个人基本信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AlterUserinfo(String _user_email, String _user_name, String _user_age, String _user_sex, String _user_qq, String _user_phone, String _user_addr, String _user_ownword)
        {
            if (Business.MainMange.alter_userinfo(_user_email, _user_name, _user_age, _user_sex, _user_qq, _user_phone, _user_addr, _user_ownword))
            {
                return Json(new { Result = true });
            }
            return Json(new { Result = false });
        }

        /// <summary>
        /// 恢复个人基本信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult RecoverUserinfo(String _user_email)
        {
            String _user_name = null;
            String _user_age = null;
            String _user_sex = null;
            String _user_qq = null;
            String _user_phone = null;
            String _user_addr = null;
            String _user_ownword = null;

            string email = _user_email;
            email = email.Trim().ToString();
            _userinfo ui = UserModelManager.instance.get(email).UserModel_userinfo;
            if (ui != null)
            {
                _user_name = ui._user_name;
                _user_age = ui._user_age;
                _user_sex = ui._user_sex;
                _user_qq = ui._user_qq;
                _user_phone = ui._user_phone;
                _user_addr = ui._user_addr;
                _user_ownword = ui._user_ownword;
                return Json(new { Result = true, _user_name, _user_age, _user_sex, _user_qq, _user_phone, _user_addr, _user_ownword });
 
            }
           return Json(new { Result = false, _user_name, _user_age, _user_sex, _user_qq, _user_phone, _user_addr, _user_ownword });
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AlterPsw(String _user_email, String _user_oldpsw, String _user_newpsw)
        {
            string email = _user_email.Trim().ToString();
            string oldpsw = _user_oldpsw.Trim().ToString();
            string newpsw = _user_newpsw.Trim().ToString();
            int num = Business.MainMange.alter_psw(_user_email, _user_oldpsw, _user_newpsw);
            if (num == 1)
            {
                return Json(new { Result = true });
            }
            else if (num == 0)
            {
                return Json(new { Result = false, Message = "没有此用户" });
            }
            else if (num == -1)
            {
                return Json(new { Result = false, Message = "原密码错误" });
            }
            return Json(new { Result = false, Message = "未知错误" });
        }


        /// <summary>
        /// 修改用户头像
        /// </summary>
        /// <param name="_user_email"></param>
        /// <param name="src"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AlterUserPhoto(String _user_email, String src)
        {
            if (Business.ImageManage.Userinfo(_user_email, src))
            {
                return Json(new { Result = true });
            }
            return Json(new { Result = false, Message = "修改失败" });
        }



        [HttpPost]
        public ActionResult AddContact(String contact_refrences_user_email, String contact_name, String contact_phone, String contact_time)
        {
            string contactid = Business.MainMange.add_contact(contact_refrences_user_email, contact_name, contact_phone, contact_time);
            if (contactid != null)
            {
                return Json(new { Result = true, Message = contactid });
            }
            return Json(new { Result = false, Message = "修改失败" });
        }


        [HttpPost]
        public ActionResult DeleteContact(String contactid)
        {
            if (Business.MainMange.delete_contact(contactid))
            {
                return Json(new { Result = true});
            }
            return Json(new { Result = false, Message = "删除失败" });
        }


        [HttpPost]
        public ActionResult AlterContact(String contactid, String contact_name, String contact_phone)
        {
            if (Business.MainMange.alter_contact(contactid, contact_name, contact_phone))
            {
                return Json(new { Result = true, Message = "修改成功" });
            }
            return Json(new { Result = false, Message = "修改失败" });
        }

        [HttpPost]
        public ActionResult GetRemoverContact(String contactid)
        {
            contact ct = new contact();
            ct = Business.MainMange.get_contact(contactid);
            if (ct != null)
            {
                string name = ct.contact_name;
                string phone = ct.contact_phone;
                string img = ct.contact_image;

                return Json(new { Result = true, contact_name = name, contact_phone = phone, contact_image = img });
            }
            return Json(new { Result = false, Message = "恢复失败" });
        }

        

        [HttpPost]
        public ActionResult AddProduct(String product_refrences_user_email, String product_name, String product_sum, String product_price, String product_decript)
        {
            string productid = Business.MainMange.add_product(product_refrences_user_email, product_name, product_sum, product_price, product_decript);
            if (productid != null)
            {
                return Json(new { Result = true, productid = productid });
            }
            return Json(new { Result = false, Message = "修改失败" });
        }

        [HttpPost]
        public ActionResult AlterProduct(String product_id, String product_name, String product_sum, String product_price, String product_decript)
        {
            if (Business.MainMange.alter_product(product_id, product_name, product_sum, product_price, product_decript))
            {
                return Json(new { Result = true, Message = "修改成功" });
            }
            return Json(new { Result = false, Message = "修改失败" });
        }

        [HttpPost]
        public ActionResult GetRemoverProduct(String product_id)
        {
            product pt = new product();
            pt = Business.MainMange.get_product(product_id);
            if (pt != null)
            {
                string product_name = pt.product_name;
                string product_sum = pt.product_sum;
                string product_price = pt.product_price;
                string product_decript = pt.product_decript;
                return Json(new { Result = true, product_name = product_name, product_sum = product_sum, product_price = product_price, product_decript = product_decript });
            }
            return Json(new { Result = false, Message = "恢复失败" });
        }
        [HttpPost]
        public ActionResult DeleteProduct(String product_id)
        {
            if (Business.MainMange.delete_product(product_id))
            {
                return Json(new { Result = true });
            }
            return Json(new { Result = false, Message = "删除失败" });
        }
        /// <summary>
        /// 获取所有产品
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetAllContact(String email)
        {
            List<product> pt = UserModelManager.instance.get(email).UserModel_product;

            if ((pt == null))
            {
                return Json(new { Result = false});
            }

            int count = pt.Count;

            if (count < 1)
            {
                return Json(new { Result = false});
            }

            List<string> product_id = new List<string>();
            List<string> product_name = new List<string>();
            List<string> product_sum = new List<string>();
            List<string> product_price = new List<string>();
            List<string> product_decript = new List<string>();
            List<string> product_refrences_user_email = new List<string>();
            for (int i = 0; i < pt.Count; i++)
            {
                string str = "" + pt[i].product_id;
                product_id.Add(str);
                string str1 = "" + pt[i].product_name;
                product_name.Add(str1);
                string str2 = "" + pt[i].product_sum;
                product_sum.Add(str2);
                string str3 = "" + pt[i].product_price;
                product_price.Add(str3);
                string str4 = "" + pt[i].product_decript;
                product_decript.Add(str4);
                string str5 = "" + pt[i].product_refrences_user_email;
                product_refrences_user_email.Add(str5);
            }
            return Json(new { Result = true, count, product_id, product_name, product_sum, product_price, product_decript, product_refrences_user_email });
        }

        /// <summary>
        /// 获取所有交易
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetAllTrade(String email)
        {
            List<trade> td = UserModelManager.instance.get(email).UserModel_trade;

            if (td == null)
            {
                return Json(new { Result = false });
            }

            int count = td.Count;

            if (count < 1)
            {
                return Json(new { Result = false });
            }

            List<string> trade_id = new List<string>();
            List<string> trade_customer = new List<string>();
            List<string> trade_refrences_product_name = new List<string>();
            List<string> trade_sum = new List<string>();
            List<string> trade_price = new List<string>();
            List<string> trade_amount = new List<string>();
            List<string> trade_decript = new List<string>();
            List<string> trade_time = new List<string>();
            List<string> trade_refrences_user_email = new List<string>();
            for (int i = 0; i < td.Count; i++)
            {
                string str = "" + td[i].trade_id;
                trade_id.Add(str);
                string str1 = "" + td[i].trade_customer;
                trade_customer.Add(str1);
                string str2 = "" + td[i].trade_refrences_product_name;
                trade_refrences_product_name.Add(str2);
                string str3 = "" + td[i].trade_sum;
                trade_sum.Add(str3);
                string str4 = "" + td[i].trade_price;
                trade_price.Add(str4);
                string str5 = "" + td[i].trade_amount;
                trade_amount.Add(str5);
                string str6 = "" + td[i].trade_decript;
                trade_decript.Add(str6);
                string str7 = "" + td[i].trade_time;
                trade_time.Add(str7);
                string str8 = "" + td[i].trade_refrences_user_email;
                trade_refrences_user_email.Add(str8);
            }
            return Json(new { Result = true, count, trade_id, trade_customer, trade_refrences_product_name, trade_sum, trade_price, trade_amount, trade_decript, trade_time, trade_refrences_user_email });
        }




        [HttpPost]
        public ActionResult AddTrade(String trade_customer, String trade_refrences_product_name, String trade_sum, String trade_price, String trade_amount, String trade_decript, String trade_time, String trade_refrences_user_email)
        {
            string tradeid = Business.MainMange.add_trade(trade_customer, trade_refrences_product_name, trade_sum, trade_price, trade_amount, trade_decript, trade_time, trade_refrences_user_email);
            if (tradeid != null)
            {
                return Json(new { Result = true, tradeid = tradeid });
            }
            return Json(new { Result = false, Message = "添加失败" });
        }

        [HttpPost]
        public ActionResult DeleteTrade(String trade_id)
        {
            if (Business.MainMange.delete_trade(trade_id))
            {
                return Json(new { Result = true });
            }
            return Json(new { Result = false, Message = "删除失败!" });
        }


        [HttpPost]
        public ActionResult GetRemoverTrade(String trade_id)
        {
            trade td = new trade();
            td = Business.MainMange.get_trade(trade_id);
            if (td != null)
            {
                string trade_customer = td.trade_customer;
                string trade_refrences_product_name = td.trade_refrences_product_name;
                string trade_sum = td.trade_sum;
                string trade_price = td.trade_price;
                string trade_amount = td.trade_amount;
                string trade_decript = td.trade_decript;
                string trade_time = td.trade_time;

                return Json(new
                {
                    Result = true,
                    trade_customer = trade_customer,
                    trade_refrences_product_name = trade_refrences_product_name,
                    trade_sum = trade_sum,
                    trade_price = trade_price,
                    trade_amount = trade_amount,
                    trade_time = trade_time,
                    trade_decript = trade_decript
                });
            }
            return Json(new { Result = false, Message = "恢复失败" });
        }

        [HttpPost]
        public ActionResult AlterTrade(String trade_id, String trade_customer, String trade_refrences_product_name, String trade_sum, String trade_price, String trade_amount, String trade_decript, String trade_time)
        {
            if (Business.MainMange.alter_trade(trade_id, trade_customer, trade_refrences_product_name, trade_sum, trade_price, trade_amount, trade_decript, trade_time))
            {
                return Json(new { Result = true, Message = "修改成功" });
            }
            return Json(new { Result = false, Message = "修改失败" });
        }

    }
}
